package com.g17.virtualpetbackend.repo;

import com.g17.virtualpetbackend.entity.Medicine;
import org.springframework.data.neo4j.core.mapping.EntityInstanceWithSource;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;

import java.util.List;

public interface MedicineRepository extends Neo4jRepository<Medicine, Long> {

    @Query("MATCH (n:medicine) RETURN n")
    List<Medicine> findAll();

    @Query("MATCH (n:medicine) WHERE n.medicine=$medicine RETURN n")
    Medicine findByMedicine(String medicine);

    @Query("MATCH (m:medicine) " +
            "WHERE id(m) = $id " +
            "RETURN m")
    Medicine findMedicineById(Long id);

    @Query("CREATE (n:medicine {medicine:$medicine, price:$price, function:$function} ) RETURN n")
    Medicine createMedicine(String medicine, String price, String function);

    @Query("MATCH (n:medicine) WHERE id(n) = $id SET n = {medicine:$medicine, price:$price, function:$function} RETURN n")
    Medicine updateMedicine(Long id, String medicine, String price, String function);

    @Query("MATCH (n:medicine) WHERE id(n) = $id DELETE n RETURN n")
    Medicine deleteMedicineById(Long id);

    @Query("MATCH (u:user {username:$username}) MATCH (m:medicine {medicine:$medicine}) CREATE (u)-[r:subscribe]->(m) RETURN r")
    EntityInstanceWithSource linkMedicineAndUser(String medicine, String username);

    @Query("MATCH (u:user)-[r:subscribe]->(m:medicine) WHERE u.username = $username return m")
    List<Medicine> findAllMedicineSubscribedByUser(String username);

    @Query("MATCH (u:user {username:$username})-[r:subscribe]->(m:medicine {medicine:$medicine}) DELETE r RETURN r")
    EntityInstanceWithSource unlinkMedicineAndUser(String medicine, String username);
}
